package com.hoshiicloud.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.query.OmnipotentParam;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.update.UpdateBatchParam;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.goods.entity.spu.BaseSpu;
import com.hoshiicloud.goods.param.spu.BaseSpuAddParam;
import com.hoshiicloud.goods.param.spu.BaseSpuQueryParam;
import com.hoshiicloud.goods.param.spu.BaseSpuUpdateParam;
import com.hoshiicloud.goods.service.BaseSpuService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/goods/base-spu")
@Api(value = "商品信息", tags = {"商品信息"})
public class BaseSpuController {
    @Autowired
    private BaseSpuService baseSpuService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询商品表
     */
    @ApiOperation(value = "分页查询商品表")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid BaseSpuQueryParam param) {
        try {
            log.info("分页查询商品表");
            QueryWrapper<BaseSpu> queryWrapper = new QueryWrapper<BaseSpu>();
            queryWrapper.lambda().eq(param.getCompanyId() != null, BaseSpu::getCompanyId, param.getCompanyId())
                    .eq(param.getPlatformId() != null, BaseSpu::getPlatformId, param.getPlatformId())
                    .eq(param.getStoreId() != null, BaseSpu::getStoreId, param.getStoreId())
                    .eq(StringUtils.isNotBlank(param.getSpuNo()), BaseSpu::getSpuNo, param.getSpuNo())
                    .like(StringUtils.isNotBlank(param.getTitle()), BaseSpu::getTitle, param.getTitle())
                    .eq(StringUtils.isNotBlank(param.getSubTitle()), BaseSpu::getSubTitle, param.getSubTitle())
                    .eq(param.getCategoryId() != null, BaseSpu::getCategoryId, param.getCategoryId())
                    .eq(param.getBrandId() != null, BaseSpu::getBrandId, param.getBrandId())
                    .eq(StringUtils.isNotBlank(param.getSaleUnit()), BaseSpu::getSaleUnit, param.getSaleUnit())
                    .eq(StringUtils.isNotBlank(param.getPacketUnit()), BaseSpu::getPacketUnit, param.getPacketUnit())
                    .eq(param.getWeight() != null, BaseSpu::getWeight, param.getWeight())
                    .eq(param.getTaxRate() != null, BaseSpu::getTaxRate, param.getTaxRate())
                    .eq(param.getGoodsLen() != null, BaseSpu::getGoodsLen, param.getGoodsLen())
                    .eq(param.getGoodsWidth() != null, BaseSpu::getGoodsWidth, param.getGoodsWidth())
                    .eq(param.getGoodsHeight() != null, BaseSpu::getGoodsHeight, param.getGoodsHeight())
                    .eq(param.getInnerBoxs() != null, BaseSpu::getInnerBoxs, param.getInnerBoxs())
                    .eq(param.getOuterBoxs() != null, BaseSpu::getOuterBoxs, param.getOuterBoxs())
                    .eq(StringUtils.isNotBlank(param.getPoAddress()), BaseSpu::getPoAddress, param.getPoAddress())
                    .eq(param.getSource() != null, BaseSpu::getSource, param.getSource())
                    .eq(StringUtils.isNotBlank(param.getExtendProperty()), BaseSpu::getExtendProperty, param.getExtendProperty())
                    .eq(StringUtils.isNotBlank(param.getExtendSpec()), BaseSpu::getExtendSpec, param.getExtendSpec())
                    .eq(param.getStatus() != null, BaseSpu::getStatus, param.getStatus())
                    .eq(param.getPublished() != null, BaseSpu::getPublished, param.getPublished())
                    .eq(param.getDeleted() != null, BaseSpu::getDeleted, param.getDeleted());
            queryWrapper.orderByDesc(StringUtils.isNotBlank(param.getDescField()),param.getDescField());
            queryWrapper.orderByAsc(StringUtils.isNotBlank(param.getAscFiled()),param.getAscFiled());
            Page<BaseSpu> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = "BaseSpuQueryParam{"+param.toString() + "},Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(baseSpuService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询商品表异常", e);
            return Response.failed("分页查询商品表异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询商品表
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询商品表")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(baseSpuService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增商品表")
    public Response save(@RequestBody @Valid BaseSpuAddParam param) {
        // Bean数据转换
        BaseSpu entity;
        try {
            entity = BeanUtils.covertType(param, BaseSpu.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(baseSpuService.save(entity));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改商品表
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改商品表")
    public Response update(@RequestBody @Valid BaseSpuUpdateParam param) {
        BaseSpu entity;
        try {
            entity = BeanUtils.covertType(param, BaseSpu.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(baseSpuService.updateById(entity));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除商品表
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除商品表")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        return Response.data(baseSpuService.deleteById(id));
    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     * @return: com.hoshiicloud.common.rpc.Response
     */
    @ApiOperation(value = "根据id集合查询商品表")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("id不能为空");
        }
        ids.sort(null);
        return Response.data(baseSpuService.getByIds(ids, StringUtils.join(ids, "")));
    }

    @PostMapping("/saveBaseSpu")
    @ApiOperation(value = "新增商品表")
    public Response saveBaseSpu(@RequestBody @Valid BaseSpuAddParam param) {
        // Bean数据转换
        BaseSpu entity;
        try {
            entity = BeanUtils.covertType(param, BaseSpu.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(baseSpuService.saveBaseSpu(entity));
    }

    @PostMapping("/updateBatch")
    @ApiOperation(value = "批量更新")
    public Response updateBatch(@RequestBody OmnipotentParam<BaseSpu> param) {
        return Response.data(baseSpuService.updateBatch(param));
    }
}
